//
//  CHLoading.swift
//  CHSwift
//
//  Created by cyptt on 2021/12/21.
//

import UIKit
import SnapKit
 public class CHLoading: UIView {

    //MARK: -lazy
    
    var defaltH = 120
    var defaltW = 120
    
    var loadingColor:UIColor = UIColor.white
    var contentColor:UIColor = UIColor.init(_colorLiteralRed: 83/255.0, green: 83/255.0, blue: 83/255.0, alpha: 1)
    var fontSize:CGFloat = 15
     
     var contentWConstraint:Constraint?
     var contentHConstraint:Constraint?
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        
    }
    
    

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var mskView = {()-> UIView in
        let window:UIWindow = keyWindow
        let view:UIView = UIView.init(frame:window.bounds )
        view.backgroundColor = UIColor.init(_colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.00)
        window.addSubview(view)
        return view
    }()
    
    
  
    lazy var contentV = {()-> UIView in
        let view:UIView = UIView.init()
        view.backgroundColor = self.contentColor
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        view.addSubview(self.spinner)
        view.addSubview(self.msgLable)
        

        return view
    }()
    
    lazy var spinner = {()-> UIActivityIndicatorView in
        let spinner:UIActivityIndicatorView = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.white)
        spinner.color = self.loadingColor
        return spinner
    }()
    
    lazy var msgLable = {()-> UILabel in
        let lable:UILabel = UILabel.init()
        lable.textColor = self.loadingColor
        lable.font = UIFont.systemFont(ofSize: self.fontSize)
        lable.numberOfLines = 0
        lable.textAlignment = NSTextAlignment.center
       
   
        return lable
    }()
    
    let keyWindow:UIWindow = {
        var window:UIWindow
        if #available(iOS 13.0, *) {
            window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last!
        }else {
            window = UIApplication.shared.keyWindow!
        }
        return window
    }()
    
    
    public func show(msg:String){
        let window:UIWindow = keyWindow
        window.addSubview(self.mskView)
        self.mskView.addSubview(self.contentV)
        
        self.spinner.startAnimating()
        self.msgLable.text = msg
        setPosition(msg: msg)
    }
    
    
    
     public func hide(){
         self.mskView.removeFromSuperview()
         
     }
     public func hide(after:Double){
         
         DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + after) {
             self.mskView.removeFromSuperview()
         }
     }
     
     

}

//MARK: --setPosition
extension CHLoading{
 
    func setPosition(msg:String){

        var spinerMargin = 0
        if msg.count == 0 {
            spinerMargin = 40
        }else{
            spinerMargin = 40
        }
        let msgCount = msg.count
        let msgW = msgCount * Int(self.fontSize)
        
        let maxW = Int(keyWindow.bounds.size.width - 80)
        var contentW = defaltW //显示视图宽度
        var contentH = defaltH // 显示视图高度
        if msgW < defaltW {
            contentW = defaltW
        } else if msgW > maxW {
            //字数宽度大于最大宽度
            contentW = maxW
            
            
            print("msgW--",msgW)
            if msgW > 3 * maxW {
                print("进来了")
                //计算高度
                print(msgW/maxW)
                print((msgW/maxW) * Int(self.fontSize))
                
                contentH = (msgW/maxW + 1) * Int(self.fontSize) + defaltH
            }
        }else {
            contentW = msgW
        }
        
        self.contentWConstraint?.update(offset: contentW)
        self.contentHConstraint?.update(offset: contentH)

        self.contentV.snp.makeConstraints { make in
            make.center.equalTo(self.mskView)
            self.contentWConstraint =  make.width.equalTo(contentW).constraint
            self.contentHConstraint =  make.height.equalTo(contentH).constraint
        }
        self.spinner.snp.makeConstraints { make in
            make.centerX.equalTo(self.mskView)
            make.top.equalTo(self.contentV).offset(spinerMargin)
        }
        self.msgLable.snp.makeConstraints { make in

            make.left.equalTo(self.contentV).offset(5)
            make.right.equalTo(self.contentV.snp.right).offset(-5)
            make.top.equalTo(self.spinner.snp.bottom).offset(10)
            make.bottom.equalTo(self.contentV.snp.bottom)

        }
        
    
      
    }
}




